package com.ruoyi.web.service.impl;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.web.domain.Disease;
import com.ruoyi.web.domain.KeyAndValue;
import com.ruoyi.web.mapper.DiseaseMapper;
import com.ruoyi.web.service.IDiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 疾病管理Service业务层处理
 *
 * @author 刘方越
 * @date 2023-02-01
 */
@Service
public class DiseaseServiceImpl implements IDiseaseService {
    @Autowired
    private DiseaseMapper diseaseMapper;
    @Autowired
    private RedisCache redisCache;


    /**
     * 项目启动时，初始化参数到缓存
     */
    @PostConstruct
    public void init() {
        refreshDisease();
    }

    private void saveDiseaseList() {
        Disease disease = new Disease();

        /*将disease表 全部插入redis*/
        List<Disease> anyDisease = diseaseMapper.selectDiseaseList(disease);
        redisCache.setCacheList("old:disease:anyDisease", anyDisease);

        disease.setPid(0L);

        List<Disease> diseaseList = diseaseMapper.selectDiseaseList(disease);
        for (Disease dis : diseaseList) {
            Long id = dis.getId();
            disease.setPid(id);
            List<Disease> ChileDiseaseList = diseaseMapper.selectDiseaseList(disease);
            if (ChileDiseaseList == null) {
                System.out.println(disease);
            }
            redisCache.setCacheList("old:disease:disease_pid_" + id, ChileDiseaseList);
            dis.setDiseaseList(ChileDiseaseList);
        }
        redisCache.setCacheList("old:disease:disease_pid_0", diseaseList);
    }

    private void refreshDisease() {
        Collection<String> cacheKeys = redisCache.keys("old:disease:*");
        redisCache.deleteObject(cacheKeys);
        saveDiseaseList();
    }

    /**
     * 查询疾病管理
     *
     * @param id 疾病管理主键
     * @return 疾病管理
     */
    @Override
    public Disease selectDiseaseById(Long id) {
        return diseaseMapper.selectDiseaseById(id);
    }

    /**
     * 查询疾病管理列表
     *
     * @param disease 疾病管理
     * @return 疾病管理
     */
    @Override
    public List<Disease> selectDiseaseList(Disease disease) {
        return diseaseMapper.selectDiseaseList(disease);
    }

    /**
     * 新增疾病管理
     *
     * @param disease 疾病管理
     * @return 结果
     */
    @Override
    public int insertDisease(Disease disease) {
        disease.setCreateTime(DateUtils.getNowDate());
        int i = diseaseMapper.insertDisease(disease);
        refreshDisease();
        return i;
    }

    /**
     * 修改疾病管理
     *
     * @param disease 疾病管理
     * @return 结果
     */
    @Override
    public int updateDisease(Disease disease) {
        disease.setUpdateTime(DateUtils.getNowDate());
        disease.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        int i = diseaseMapper.updateDisease(disease);
        refreshDisease();
        return i;
    }

    /**
     * 批量删除疾病管理
     *
     * @param ids 需要删除的疾病管理主键
     * @return 结果
     */
    @Override
    public int deleteDiseaseByIds(String ids) {
        String loginName = ShiroUtils.getSysUser().getLoginName();
        int i = diseaseMapper.deleteDiseaseByIds(Convert.toStrArray(ids), loginName);
        refreshDisease();
        return i;
    }

    /**
     * 删除疾病管理信息
     *
     * @param id 疾病管理主键
     * @return 结果
     */
    @Override
    public int deleteDiseaseById(Long id) {
        String loginName = ShiroUtils.getSysUser().getLoginName();
        int i = diseaseMapper.deleteDiseaseById(id, loginName);
        refreshDisease();
        return i;
    }

    /**
     * 查询疾病管理树列表
     *
     * @return 所有疾病管理信息
     */
    @Override
    public List<Ztree> selectDiseaseTree() {
        List<Disease> diseaseList = diseaseMapper.selectDiseaseList(new Disease());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Disease disease : diseaseList) {
            Ztree ztree = new Ztree();
            ztree.setId(disease.getId());
            ztree.setpId(disease.getPid());
            ztree.setName(disease.getName());
            ztree.setTitle(disease.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public List<KeyAndValue> diseaseKeyAndValue(Disease disease) {
        return diseaseMapper.diseaseKeyAndValue(disease);
    }

    @Override
    public List<KeyAndValue> diseaseKeyValue(Disease disease) {

        return diseaseMapper.diseaseKeyValue(disease);
    }

    @Override
    public Disease selectPidText(Disease disease) {

        return diseaseMapper.selectPidText(disease);
    }

    @Override
    public List<Map<String, Object>> getDiseaseData(Disease disease) {

        if (disease.getPid() == null) {
            disease.setPid(0L);
        }

        List<Disease> diseases = diseaseMapper.selectDiseaseList(disease);

        List<Map<String, Object>> diseaseData = new ArrayList<>();

        diseases.forEach(item -> {

            Map<String, Object> diseaseMap = new HashMap<>();
            diseaseMap.put("id", item.getId());
            diseaseMap.put("name", item.getName());

            Disease newDisease = new Disease();
            newDisease.setPid(item.getId());

            List<Disease> diseaseList = diseaseMapper.selectDiseaseList(newDisease);

            diseaseMap.put("diseaselist", diseaseList);

            diseaseData.add(diseaseMap);

        });

        return diseaseData;
    }

    @Override
    public List<Disease>    selectAnyDisease(){
        return diseaseMapper.selectAnyDisease();
    }
}
